<template>
  <div id="user">
    <!-- 绑定一个自定义事件，更新或添加数据成功后及时更新页面数据 -->
    <AddUpdate @getUser="getUserData" :editRow="editRow" />

    <div class="buttons">
      <el-button size="small" type="primary" @click="addUser">添加</el-button>
      <span class="search">
        <el-form :inline="true" :model="searchInfo" class="demo-form-inline">
          <el-form-item label="按名查找">
            <el-input v-model="searchInfo.name"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSearch">查询</el-button>
          </el-form-item>
        </el-form>
      </span>
    </div>

    <el-table :data="userData" border style="width: 100%; height: 500px">
      <el-table-column align="center" prop="name" label="姓名" width="193">
      </el-table-column>
      <el-table-column align="center" prop="age" label="年龄" width="120">
      </el-table-column>
      <el-table-column align="center" prop="sex" label="性别" width="120">
      </el-table-column>
      <el-table-column align="center" prop="birth" label="出生日期" width="200">
      </el-table-column>
      <el-table-column align="center" prop="addr" label="地址" width="500">
      </el-table-column>
      <el-table-column align="center" label="操作" width="200">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <Pagination @pageChange="pageChange" />
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import AddUpdate from "@/components/AddUpdate";
import Pagination from "@/components/Pagination";
export default {
  name: "User",
  components: { AddUpdate, Pagination },
  data() {
    return {
      
      searchInfo:{
        name:''
      },
      editRow: {},
      userData: [],
    };
  },
  mounted() {
    this.getData();
  },
  computed: {
    ...mapGetters("user", ["userList"]),
  },
  methods: {
    addUser() {
      this.$store.commit("user/ADDUSER");
    },
    handleEdit(index, row) {
      // 深度拷贝
      this.editRow = JSON.parse(JSON.stringify(row));
      this.$store.commit("user/EDITUSER");
    },
    async handleDelete(index, row) {
      if (confirm(`确定删除${row.name}吗？`)) {
        const res = await this.$store.dispatch("user/deleteUser", row.id);
        res && this.getData();
      }
    },
    async getData(data) {
      const res = await this.$store.dispatch("user/getUserList",data);
      if (res) {
        this.userData = this.userList;
      }
    },
    // 自定义事件回调
    getUserData() {
      // console.log('自定义事件');
      this.getData();
    },
    // 分页器
    async pageChange(pageInfo) {
      // const res = await this.$store.dispatch("user/getUserList", pageInfo);
      // if (res) {
      //   this.userData = this.userList;
      // }
      this.getData(pageInfo)
    },
    async onSearch(){
      const params = {name:this.searchInfo.name}
      this.getData(params)
    }
  },
};
</script>

<style lang='less' scoped>
#user {
  .buttons {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
}
</style>